package com.jf.springcloud.controller;

import com.jf.springcloud.entity.Member;
import com.jf.springcloud.util.Msg;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2025-02-14
 * Time:21:49
 */
@RequestMapping("/member")
@RestController
public class MemberController {
    private final static String MEMBER_SERVICE_PROVIDER_URL = "http://MEMBER-SERVICE-PROVIDER";
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private DiscoveryClient discoveryClient;

    @PostMapping("/save")
    public Msg save(Member member) {
        return restTemplate.postForObject(MEMBER_SERVICE_PROVIDER_URL + "/member/save", member, Msg.class);
    }
    @GetMapping("/get/{id}")
    public Msg get(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(MEMBER_SERVICE_PROVIDER_URL + "/member/get/" + id, Msg.class);
    }
    @GetMapping("/discovery")
    public Msg discovery() {
        List<String> services = discoveryClient.getServices();
        Msg msg = Msg.success();
        for (String service : services) {
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            msg.addData(service, instances);
        }
        return msg;
    }
}
